package cn.zhz.service;

import cn.zhz.dao.StreetDao;
import cn.zhz.pojo.Street;
import org.hibernate.Transaction;
import org.hibernate.TransactionException;

public class StreetService {

    private StreetDao streetDao = new StreetDao();

    public void addStreet(Street street){
        Transaction tx = null;
        try{
            tx = streetDao.currentSession().beginTransaction();
            streetDao.save(street);
            tx.commit();
        }catch (TransactionException e){
            e.printStackTrace();
            if (tx != null){
                tx.rollback();
            }
        }
    }

    public void updateStreet(Street street){
        Transaction tx = null;
        try{
            tx = streetDao.currentSession().beginTransaction();
            streetDao.update(street);
            tx.commit();
        }catch (TransactionException e){
            e.printStackTrace();
            if (tx != null){
                tx.rollback();
            }
        }
    }

    public void delStreet(Street street){
        Transaction tx = null;
        try{
            tx = streetDao.currentSession().beginTransaction();
            streetDao.delete(street);
            tx.commit();
        }catch (TransactionException e){
            e.printStackTrace();
            if (tx != null){
                tx.rollback();
            }
        }
    }

    public StreetDao getStreetDao() {
        return streetDao;
    }

    public void setStreetDao(StreetDao streetDao) {
        this.streetDao = streetDao;
    }
}
